package eventdisplay;

import processing.core.PApplet;
import processing.core.PVector;

public class Cylinder {
	
	PApplet parent;

	double angle;
	double lengthZ;
	PVector vertices[][];
	double angleInc;
	int noPts;

	Cylinder(PApplet p, double radius, int pts, double frontZ, double backZ) {
		parent = p;
		noPts = pts;
		radius = -radius / Math.cos(360 / (pts * 2));
		lengthZ = frontZ;

		// initialize vertex arrays
		vertices = new PVector[2][pts + 1];
		for (int i = 0; i < 2; i++) {
			angle = 22.5;
			for (int j = 0; j <= pts; j++) {
				vertices[i][j] = new PVector();
				vertices[i][j].x = (float) (Math.cos(Math.toRadians(angle)) * radius);
				vertices[i][j].y = (float) (Math.sin(Math.toRadians(angle)) * radius);
				vertices[i][j].z = (float) lengthZ;
				angle += 360.0 / pts;
			}
			lengthZ = backZ;
		}
	}

	void drawCylinder(int col) {
		parent.strokeWeight(1);
		parent.stroke(col);
		parent.noFill();
		for (int j = 0; j <= noPts - 1; j++) {
			parent.beginShape();
			parent.vertex(vertices[0][j].x, vertices[0][j].y, vertices[0][j].z);
			parent.vertex(vertices[1][j].x, vertices[1][j].y, vertices[1][j].z);
			parent.vertex(vertices[1][j + 1].x, vertices[1][j + 1].y,vertices[1][j + 1].z);
			parent.vertex(vertices[0][j + 1].x, vertices[0][j + 1].y,vertices[0][j + 1].z);
			parent.endShape(parent.CLOSE);
		}
	}

}

